package de.backtomedieval.buildings;

import de.backtomedieval.R;
import de.backtomedieval.game.Costs;
import de.backtomedieval.game.Earning;
import de.backtomedieval.main.Game;

public class Defence_Wall extends AbstractBuilding {

	private static int _healthpoints = 0;
	private static int _defense = 50;
	private static Earning earnSetting = null;
	private static Costs costSetting = null;
	
	private int hasDamage = 0;
	
	public Defence_Wall() {
		super(_healthpoints, _defense);

		if (costSetting == null){
			costSetting = new Costs();
			costSetting.setStoneCost(500);
		}
		this.setCosts(costSetting);
	}

	
	@Override
	int getDefenseBonus() {
		return 0;
	}
	@Override
	int getHealthpointsBonus() {
		return 0;
	}
	@Override
	int getIncomeBonus() {
		return 0;
	}
	
	@Override
	public String getName() {
		return Game.getString(R.string.building_defencewall);
	}
	
	@Override
	public void levelup() {
		super.levelup();
		setHealthpoints(getHealthpoints() + getUpgradeFormel(getLevel()));
	}
	
	public int getUpgradeFormel(int level) {
		return 300 + level *200;
	}
	
	@Override
	public int hurt(int damage) {
		if (!isBuilded()) return 0;
		if (getHealthpoints() <= damage) {
			int lostHealthpoints = getHealthpoints();
			setHealthpoints(0);
			this.setLevel(0);
			this.hasDamage=0;
			this.setBuilded(false);
			return lostHealthpoints;
		}
		int toDamage = damage;
		do {
			int maxDamageAtLevel = getUpgradeFormel(getLevel());
			if (toDamage + hasDamage >= maxDamageAtLevel) {
				toDamage = maxDamageAtLevel-hasDamage;
				this.setLevel(getLevel()-1);
				hasDamage = 0;
			} else {
				hasDamage = hasDamage + toDamage;
				toDamage = 0;
			}
			
		} while (toDamage>0);
		setHealthpoints(getHealthpoints() - damage);
		return damage;
	}
	
	/*public void setLevelBack() {
		if (this.getLevel()<2) {return;}
		do {
			int hp = this.getLevel();
		} while(this.getLevel()>1 && true);
	} */
	
	@Override
	public void setLevel(int level) {
		super.setLevel(level);
		int hp = 0;
		for (int i=0; i<level;i++) {
			hp = hp + getUpgradeFormel(i+1);
		}
		setHealthpoints(hp);
	}
	
	
	
	@Override
	public int getImage() {
		return R.drawable.defence_wall;
	}


	public void repair() {
		if (isBuilded()) {
			this.setHealthpoints(this.getHealthpoints() + hasDamage);
			hasDamage = 0;
		}
	}
}
